package com.softaria.spkiller.layers.classificator;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.softaria.spkiller.layers.LayerIdentifier;
import com.softaria.spkiller.metadata.ClassIdentifier;
import com.softaria.spkiller.metadata.ClassMetaData;

class ClassificatorCacheWrapper implements Classificator {

	private Classificator classificator;
	
	private Map<ClassIdentifier, LayerIdentifier> layerByClass = new HashMap<ClassIdentifier, LayerIdentifier>();
	
	private Set<LayerIdentifier> allLayers = null;
	
	public ClassificatorCacheWrapper(Classificator classificator) {
		super();
		this.classificator = classificator;
	}


	@Override
	public LayerIdentifier classificate(ClassIdentifier id) {
		LayerIdentifier layer = layerByClass.get(id);
		
		if(layer==null) {
			layer = classificator.classificate(id);
			if(layer!=null) {
				layerByClass.put(id, layer);
			}
		}
		
		return layer;
	}

	@Override
	public LayerIdentifier classificate(ClassMetaData metaData) {
		return classificate(metaData.getIdentifier());

	}

	public Collection<ClassIdentifier> getAllClasses() {
		return layerByClass.keySet();
	}
	
	public Collection<LayerIdentifier> getAllLayers() {
		if(allLayers==null) {
			allLayers = new HashSet<LayerIdentifier>(layerByClass.values());
		}
		
		return allLayers;
	}


	

}
